<template>
  <div class="app-container">
    <Search-content :queryParams="queryParams" labelWidth="120px" @query="handleQuery" @reset="resetQuery">
      <el-col :span="6">
        <el-form-item label="车牌号:">
          <el-input v-model.trim="queryParams.carNo" clearable :maxlength="30" placeholder="请输入车牌号"/>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="车架号:">
          <el-input v-model.trim="queryParams.vinNo" clearable :maxlength="30" placeholder="请输入车架号"/>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="品牌/车系/车型:">
          <linkage-select @changeNode="linkageChange" v-model="linkageList" placeholder="请选择品牌/车系/车型"/>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="客户名称:">
          <el-input v-model.trim="queryParams.customerName" :maxlength="30" clearable placeholder="请输入客户名称"/>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="车辆产权城市:">
          <el-input v-model.trim="queryParams.propertyRightName" :maxlength="30" clearable
                    placeholder="请输入车辆产权城市"/>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="关联订单:">
          <el-input v-model.trim="queryParams.orderNo" :maxlength="30" clearable placeholder="请输入关联订单"/>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="产品方案:">
          <el-input v-model.trim="queryParams.schemeName" :maxlength="30" clearable placeholder="请输入产品方案"/>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="创建人:">
          <el-input v-model.trim="queryParams.createBy" :maxlength="30" clearable placeholder="请输入创建人"/>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="创建时间:">
          <el-date-picker v-model="valueDate" type="daterange" placeholder="请选择日期" range-separator="-"
                          @change="handleTime" value-format="yyyy-MM-dd"
                          start-placeholder="开始日期" end-placeholder="结束日期" style="width: 100%;"/>
        </el-form-item>
      </el-col>
    </Search-content>

    <div class="card-wrap card-padding">
      <div class="table-title-wrap justify-end">
        <el-button size="mini" @click="handleNew"
                   v-hasPermi="['purchaseHandle:purchaseHandleList:new']" type="primary">
          新增申请
        </el-button>

        <el-button size="mini" type="primary" @click="handleDerive(row, 'derive')"
                   v-hasPermi="['purchaseHandle:purchaseHandleList:derive']">
          导出
        </el-button>
      </div>

      <table-page :loading="loading" :dataList="dataList" :total="total" :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize" @setSizeCurrent="handleList" border>

        <template slot="tableColAc">
          <el-table-column label="操作" align="center" fixed="left" width="150">
            <template #default="{row}">
              <el-button size="mini" type="text" @click="handleView(row, 'view')"
                         v-hasPermi="['purchaseHandle:purchaseHandleList:view']">
                查看
              </el-button>

              <!--自动发起-->
              <el-button size="mini" type="text" @click="handleDisposal(row,'view')"
                         v-if="[0,3].includes(Number(row.auditStatus)) && Number(row.source)=== 0"
                         v-hasPermi="['purchaseHandle:purchaseHandleList:disposal']">
                处置车辆
              </el-button>

              <!--驳回到发起人-->
              <el-button size="mini" type="text" @click="handleDisposal(row,'edit')"
                         v-if=" Number(row.auditStatus)=== 3 && Number(row.source)=== 1 "
                         v-hasPermi="['purchaseHandle:purchaseHandleList:disposalB']">
                处置车辆
              </el-button>

              <el-button size="mini" type="text" @click="handleApplyInvoice(row)"
                         v-if="Number(row.auditStatus) === 2 && Number(row.invoiceStatus)=== 0"
                         v-hasPermi="['purchaseHandle:purchaseHandleList:applyInvoice']">
                申请开票
              </el-button>

              <el-button size="mini" type="text" @click="handleInvoice(row)"
                         v-if="Number(row.auditStatus) === 2 && Number(row.invoiceStatus)=== 1"
                         v-hasPermi="['purchaseHandle:purchaseHandleList:invoice']">
                开票
              </el-button>

              <el-button size="mini" type="text" @click="handleViewInvoice(row)"
                         v-if="Number(row.auditStatus) === 2 && Number(row.invoiceStatus)=== 2"
                         v-hasPermi="['purchaseHandle:purchaseHandleList:viewInvoice']">
                查看发票
              </el-button>

            </template>
          </el-table-column>

        </template>

        <template slot="tableCol">
          <el-table-column align="center" label="租购过户编号" min-width="200" show-overflow-tooltip
                           prop="leasePurchaseTransferNo"/>

          <el-table-column align="center" label="车牌号" min-width="140" show-overflow-tooltip prop="carNo"/>

          <el-table-column align="center" label="客户名称" min-width="140" show-overflow-tooltip prop="customerName"/>

          <el-table-column align="center" label="车架号" min-width="180" show-overflow-tooltip prop="vinNo"/>

          <el-table-column label="品牌/车型/车款" show-overflow-tooltip min-width="400">
            <template #default="{row}">
              {{ row.brandName }}{{ row.modelName ? '/' + row.modelName : '' }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="车辆产权公司" show-overflow-tooltip min-width="160"
                           prop="propertyRightName"/>

          <el-table-column align="center" label="车辆归属公司" show-overflow-tooltip min-width="160" prop="ascription"/>

          <el-table-column align="center" label="审核节点" show-overflow-tooltip min-width="130">
            <template #default="{row}">
              {{ row.auditTask || '-'}}
            </template>
          </el-table-column>

          <el-table-column align="center" label="审核状态" show-overflow-tooltip min-width="130">
            <template #default="{row}">
              {{ row.auditFlowStatus || '-'}}
            </template>
          </el-table-column>

          <el-table-column align="center" label="关联订单" show-overflow-tooltip min-width="200"
                           prop="orderNo">
            <template #default="{row}">
              <el-button type="text" @click="handleTo(row.orderNo)"> {{ row.orderNo || '-' }}</el-button>
            </template>
          </el-table-column>

          <el-table-column align="center" label="产品方案" show-overflow-tooltip min-width="160"
                           prop="schemeName">
            <template #default="{row}">
              {{ row.schemeName || '-'}}
            </template>
          </el-table-column>

          <el-table-column align="center" label="创建人" show-overflow-tooltip min-width="100"
                           prop="createBy">
            <template #default="{row}">
              {{ row.createBy || '-'}}
            </template>
          </el-table-column>

          <el-table-column align="center" label="创建时间" show-overflow-tooltip min-width="120">
            <template #default="{row}">
              {{ row.createTime ? $dayjs(row.createTime).format('YYYY-MM-DD') : '-' }}
            </template>
          </el-table-column>

        </template>
      </table-page>
    </div>

    <invoice ref="refInvoice" @reset="reset"/>
  </div>
</template>

<script>
import LinkageSelect from "@/views/components/linkageSelect.vue";
import SearchContent from "@/components/SearchContent/index.vue";
import invoice from "../../components/purchaseHandle/invoice.vue"
import * as apis from '@/api/vehicleDisposal/purchaseHandle.js'
import * as outInventoryApi from '@/api/carInventory/outInventory'

export default {
  name: 'PurchaseHandleList',//租购过户处理
  components: {SearchContent, LinkageSelect, invoice},
  watch: {},
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carNo: "",
        vinNo: "",
        brandName: "",
        seriesName: "",
        modelName: "",
        customerName: "",
        propertyRightName: "",
        orderNo: "",
        schemeName: "",
        createBy: "",
        startTime: "",
        endTime: ""
      },
      linkageList: [],
      dataList: [],
      total: 0,
      loading: false,
      valueDate: [],
    };
  },
  created() {
    this.handleList()
  },
  activated() {
    this.handleList()
  },
  methods: {
    async handleTo(val) {
      if (!val) return
      try {
        let res = await outInventoryApi.outOrderDetails(val)
        if (res.code === 200) {
          this.$router.push({
            name: 'SuborderDetail',
            query: {
              id: res.data.orderCarId,
              orderId: res.data.orderCode,
            },
          })
        }
      } catch (e) {
        console.error(e)
      }
    },

    handleTime(val) {
      if (val) {
        this.queryParams.startTime = val[0]
        this.queryParams.endTime = val[1]
      }
    },

    handleQuery() {
      this.queryParams.pageNum = 1;
      this.handleList();
    },

    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        carNo: "",
        vinNo: "",
        brandName: "",
        seriesName: "",
        modelName: "",
        customerName: "",
        propertyRightName: "",
        orderNo: "",
        schemeName: "",
        createBy: ""
      };
      this.linkageList = []
      this.valueDate = []
      this.handleList();
    },

    linkageChange(val) {
      this.linkageList = val;
      if (val) {
        this.queryParams.brandName = val[0] ? val[0].name : "";
        this.queryParams.seriesName = val[1] ? val[1].name : "";
        this.queryParams.modelName = val[2] ? val[2].name : "";
      } else {
        this.queryParams.brandName = "";
        this.queryParams.seriesName = "";
        this.queryParams.modelName = "";
      }
      this.$forceUpdate();
    },

    //列表
    async handleList() {
      this.loading = true;
      try {
        const res = await apis.getList(this.queryParams);
        this.dataList = res.rows;
        this.total = res.total;
      } catch (e) {
        console.error("获取失败", e);
      } finally {
        this.loading = false;
      }
    },

    //新增
    handleNew() {
      this._toPage({
        path: "/VehicleDisposal/RentPurchaseTransfer/PurchaseHandleNew",
        name: 'PurchaseHandleNew',
        query: {
          type: "edit",
        }
      })
    },

    //查看
    handleView(row) {
      this._toPage({
        path: "/VehicleDisposal/RentPurchaseTransfer/PurchaseHandleDetails",
        name: 'PurchaseHandleDetails',
        query: {
          type: "view",
          id: row.leasePurchaseTransferId,
          flowId: row.flowId
        }
      })
    },

    //
    handleDisposal(row, type) {
      if (type === 'view') {
        this._toPage({
          path: "/VehicleDisposal/RentPurchaseTransfer/PurchaseHandleDisposal",
          name: 'PurchaseHandleDisposal',
          query: {
            type: 'view',
            id: row.leasePurchaseTransferId,
            flowId: row.flowId
          }
        })
      }

      if (type === 'edit') {
        this._toPage({
          path: "/VehicleDisposal/RentPurchaseTransfer/PurchaseHandleNew",
          name: 'PurchaseHandleNew',
          query: {
            type: "edit",
            id: row.leasePurchaseTransferId,
            flowId: row.flowId
          }
        })
      }
    },

    //导出
    handleDerive() {
      this.download(
          "/lease/carLeasePurchaseTransfer/export",
          {
            ...this.queryParams,
          },
          `车辆租购过户列表_${new Date().getTime()}.xlsx`
      );
    },

    //申请开票
    handleApplyInvoice(row) {
      this.$refs.refInvoice.handleOpen('申请开票', row, true)
    },

    //开票
    handleInvoice(row) {
      this.$refs.refInvoice.handleOpen('开票', row, false)
    },

    //查看发票
    handleViewInvoice(row) {
      this.$refs.refInvoice.handleOpen('查看发票', row, false)
    },

    reset() {
      this.queryParams.pageNum = 1;
      this.handleList();
    }
  },
};
</script>

<style scoped lang=""></style>


